package auth

import(
    . "nightowl/config"
	"crypto/hmac"
    "crypto/sha256"
    "encoding/hex"
    "net/url"
    "fmt"
)


const (
    SIGN_AK_HEADER_NAME = "x-access-key"
    SIGN_TIMESTAMP_HEADER_NAME = "x-timestamp"
    SIGN_SIGN_HEADER_NAME = "x-sign"
) 
// 生成HMAC-SHA256签名（16进制）
func HmacSha256( data, sk string) string {
    h := hmac.New(sha256.New, []byte(sk))
    h.Write([]byte(data))
    return hex.EncodeToString(h.Sum(nil))
}
func Sign( data string ) string {
    return HmacSha256(data, ENV_CONFIG.AuthConfig.SecretKey)
}

func GenSignString(ak, timestamp, method, urlstr, body string) string{
    // 解析 URL
    parsedURL, err := url.Parse(urlstr)
    if err != nil {
        fmt.Println("err:", err)
        return ""
    }

    // 获取路径 (Path)
    // fmt.Println("Path:", parsedURL.Path)
    // fmt.Println("Query String:", parsedURL.RawQuery) 
    signData := fmt.Sprintf("%s%s%s%s%s%s",
        timestamp,
        ak,
        method,
        parsedURL.Path,
        parsedURL.RawQuery,
        body,
    )
    return signData
}
func BuildSignStr(timestamp, method, urlstr, body string) string{
	return GenSignString(ENV_CONFIG.AuthConfig.AccessKey, timestamp, method, urlstr, body)
}